package com.beiding.controller;

import com.beiding.exception.BadRequestException;
import com.beiding.service.AuthService;
import com.beiding.service.OrderService;
import com.beiding.pojo.order.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.List;

@RestController
@RequestMapping("/landlord")
public class LandlordController {


    /*


       订单的持有者为房客

       房东不能删除订单,只有房客才可以删除.在订单付款后以及结束前房客不能删除订单


     */


    private OrderService orderService;

    @Autowired
    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }


    //房东支持的操作:

    //1. 受理

    @PostMapping("/transact/accept")
    @Secured("ROLE_LANDLORD")
    void accept(Principal principal, String id) {

        orderService.checkRoomHolder(id, principal.getName());

        orderService.accept(id);
    }


    /*

        办理结果有四种可能:
            1.成功(success)
            2.失败:
                            ①   :未锁死(failed)
                            ②   :锁死(lock)
                            ③   :终止(termination)
     */


    //2. 办理入住
    @PostMapping("/transact/checkIn")
    @Secured("ROLE_LANDLORD")
    String checkIn(Principal principal, String id, String checkInKey) {

        //权限检查
        orderService.checkRoomHolder(id, principal.getName());

        //返回办理结果
        return orderService.checkIn(id, checkInKey);

    }


    //3. 获取自己未处理的订单
    @GetMapping("/orders")
    @Secured("ROLE_LANDLORD")
    List<Order> orders(Principal principal) {
        return orderService.getOrdersByRoomHolder(principal.getName());
    }

    //5. 办理退款申请
    @PostMapping("/transact/refund")
    @Secured("ROLE_LANDLORD")
    void refund(Principal principal, String id, boolean agree) {
        orderService.checkRoomHolder(id, principal.getName());

        if (agree) {
            orderService.agreeRefund(id);
        } else {
            orderService.disagreeRefund(id);
        }
    }


    //6. 办理退还押金
    @PostMapping("/transact/refund-deposit")
    @Secured("ROLE_LANDLORD")
    void refundDeposit(Principal principal, String id) {
        orderService.checkRoomHolder(id, principal.getName());

        orderService.agreeRefundDeposit(id);

    }


    //7. 申请扣押金仲裁
    @PostMapping("/transact/withholdDeposit/arbitration")
    @Secured("ROLE_LANDLORD")
    void arbitration(Principal principal, String id) {
        orderService.checkRoomHolder(id, principal.getName());

        orderService.arbitrationWithholdDeposit(id);

    }


    //4. 解锁操作

    @GetMapping("/order/unlock")
    String unlock(String token) {

        orderService.unlockOrder(token);

        return "已解锁，您还有三次机会输入入住码！";
    }

    private AuthService authService;

    @Autowired
    public void setAuthService(AuthService authService) {
        this.authService = authService;
    }

    //8. 重新发送订单激活邮件
    @PostMapping("/resent-unlock-email")
    void resentUnlockEmail(Principal principal, String email, String orderId) {

        //首先检查邮件是否正确
        if (!authService.check(principal.getName(), email)) {
            throw new BadRequestException("邮件错误");
        }

        orderService.sentUnlockEmail(principal.getName(), email, orderId);

    }


}
